package com.bianguo.android.tencentpushdemo.adapters;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/7/4.
 */

public class TopPagerAdapter extends PagerAdapter {

    private ArrayList<? extends View> views;

    public TopPagerAdapter(ArrayList<? extends View> views) {
        this.views = views;
    }

    @Override
    public int getCount() {
        return views != null ? views.size() : 0;
    }

    @Override
    public boolean isViewFromObject(View view, Object o) {
        return view == o;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        if (views.size() > 0
                && views.get(position % views.size()).getParent() != null) {
            ((ViewPager) views.get(position % views.size()).getParent())
                    .removeView(views.get(position % views.size()));
        }
        try {
            ((ViewPager) container).addView(
                    views.get(position % views.size()), 0);
        } catch (Exception e) {
        }
        return views.get(position % views.size());
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView(views.get(position
                % views.size()));
    }
}
